package com.atguigu.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 文件上传的注意点：
 * 1.前端注意点
 * 1.1 表单请求方式 必须是 post
 * 1.2 表单 from 必须添加属性  enctype="multipart/form-data"
 * 2.后端注意点
 * 2.1 springmvc 配置文件中配置 文件上传解析器
 * <bean id="multipartResolver"
 * class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
 * 2.2 项目 pom 文件中必须加载文件上传 jar
 *    <groupId>commons-fileupload</groupId>
 *    <artifactId>commons-fileupload</artifactId>
 *    <version>1.3.1</version>
 * 2.3 控制器的参数 MultipartFile avator, 的 avator 必须是前端表单 文件域的 name 属性
 *
 */
@Controller
public class FileUpAndDow {
    @RequestMapping("/test/down")
    public ResponseEntity<byte[]> testResposeEntity(HttpSession session) throws IOException {
        String oraginFileName="333.png";
        ServletContext servletContext = session.getServletContext();
        String realPath = servletContext.getRealPath("download");
        realPath=realPath+System.getProperty("file.separator")+oraginFileName;
        System.out.println("realPath"+realPath);
        InputStream inputStream = new FileInputStream(realPath);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes);
        MultiValueMap<String,String> headers = new HttpHeaders();
        UUID uuid = new UUID(1, 1);
        String fileName=UUID.randomUUID().toString()+oraginFileName.substring(oraginFileName.lastIndexOf("."));
        headers.add("Content-Disposition","attachement;filename="+fileName);
        HttpStatus httpStatus = HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes, headers, httpStatus);
        inputStream.close();
        return responseEntity;
    }

    @RequestMapping("/test/upload")//千万注意，这里的 avator 是上传表单的文件域的属性名称，错了就空指针异常
    public String testUp(MultipartFile avator, HttpSession session) throws IOException {
        String originalFilename = avator.getOriginalFilename();
        System.out.println(originalFilename);
        String ext=originalFilename.substring(originalFilename.lastIndexOf("."));
        //处理文件名重名问题
        String fileName=UUID.randomUUID().toString()+ext;
        //获取服务器中 avator 目录的路径
        ServletContext servletContext = session.getServletContext();
        String uploads = servletContext.getRealPath("uploads");
        File file = new File(uploads);
        if (!file.exists()){
            file.mkdir();
        }
        String finalPath=uploads+File.separator+fileName;
        avator.transferTo(new File(finalPath));

        return "success";
    }
}
